package com.ai.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.cache.Constants;
import com.ai.web.service.WxMenuService;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.ResultModel;
import com.ai.web.vo.WxMenuV;
import com.tiny.persistence.domain.WxMenu;

/**
 * 前端菜单
* @ClassName: wxMenuController 
* @Description: TODO 
* @author:  
* @date: 2014年3月24日
*
 */
@Controller
@RequestMapping("/wxMenu")
public class WxMenuController extends BaseController {
    

    private static Logger logger = Logger.getLogger(WxMenuController.class);
    
    @Autowired
    private WxMenuService wxMenuService;
    
    @RequestMapping(value = "/list")
    public String list() {
        
        logger.info("hello......test");
        return "wxMenu/wxMenu_list";
    }
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        QueryData qd = wxMenuService.queryListAndCount(conditionMap);
        
        return qd;
    }
    
    
    @RequestMapping(value = "/new")
    public String newwxMenu(Model model) {
        
        WxMenuV wxMenuV = new WxMenuV();
        model.addAttribute(wxMenuV);
        
        return "wxMenu/wxMenu_template";
    }
    @RequestMapping(value = "/view/{id}")
    public String view(@PathVariable("id") int id, Model model)
            throws Exception {
        
        WxMenuV wxMenuV = wxMenuService.getWxMenu(id);
        logger.info("----- getUser:" + wxMenuV);
        
        if (wxMenuV == null) {
            // exception redirect error page
            // TODO
        }
        model.addAttribute(wxMenuV);
        
        return "wxMenu/wxMenu_template";
    }
    @RequestMapping(value = "/update/{id}")
    public String updatewxMenu(@PathVariable("id") int id, Model model)
            throws Exception {
        
        WxMenuV wxMenuV = wxMenuService.getWxMenu(id);
        logger.info("----- getUser:" + wxMenuV);
        
        if (wxMenuV == null) {
            // exception redirect error page
            // TODO
        }
        model.addAttribute(wxMenuV);
        
        return "wxMenu/wxMenu_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel savewxMenu(WxMenuV wxMenuV) throws Exception {
        
        logger.info("-----save:" + wxMenuV);
        wxMenuV.setMenuName("菜单一");
        wxMenuV.setActionUrl("urlaction");
        wxMenuV.setParentId(0);
        wxMenuV.setType("item");
        
        ResultModel rm = wxMenuService.saveWxMenu(wxMenuV);
        
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deletewxMenu(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> userIds = getDeleteIds(delIds);
        ResultModel rm = wxMenuService.deleteWxMenus(userIds);
        
        return rm;
    }
    
    @ResponseBody
    @RequestMapping(value = "/menus", method = RequestMethod.GET)
    public List<Map<String,Object>> findMenuList(HttpServletRequest request)
            throws Exception {
    	List<WxMenu> list = wxMenuService.findAllMenuList();
    	List<Map<String, Object>> menus = new ArrayList<Map<String,Object>>();
    	List<WxMenu> tempList = getTempList(list);
    	for(WxMenu menu:list){
    		if(menu.getParentId() == -1){
    	    	Map<String, Object> map = new HashMap<String, Object>();
    	    	map.put("id", menu.getId());
    	    	map.put("name", menu.getMenuName());
    	    	map.put("type", menu.getType());
    	    	map.put("actionUrl", menu.getActionUrl());
    	    	tempList.remove(menu);
    	    	if(Constants.MENU_TYPE_FOLDER.equals(menu.getType())){
    	    		map.put("children", getChildren(tempList, menu.getId(), new ArrayList<Map<String,Object>>()));
    	    	}
    	    	menus.add(map);
    		}
    	}
    	return menus;
    }

	private List<WxMenu> getTempList(List<WxMenu> list) {
		List<WxMenu> tempList = new ArrayList<WxMenu>();
		for(WxMenu menu:list){
			tempList.add(menu);
		}
		return tempList;
	}

	private List<Map<String,Object>> getChildren(List<WxMenu> list, int menuId, List<Map<String,Object>> children) {
		List<WxMenu> tempList = getTempList(list);
		for(WxMenu menu:list){
			if(menu.getParentId() == menuId){
				Map<String, Object> map = new HashMap<String, Object>();
    	    	map.put("id", menu.getId());
    	    	map.put("name", menu.getMenuName());
    	    	map.put("type", menu.getType());
    	    	map.put("actionUrl", menu.getActionUrl());
    	    	tempList.remove(menu);
    	    	if(Constants.MENU_TYPE_FOLDER.equals(menu.getType())){
    	    		map.put("children", getChildren(tempList, menu.getId(), new ArrayList<Map<String,Object>>()));
    	    	}
    	    	children.add(map);
			}
		}
		return children;
	}
    
}
